//
//  TapActionOneDetailViewController.m
//  礼物
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "TapActionOneDetailViewController.h"
#import "GoodStoreDetailCollectionViewCell.h"
@interface TapActionOneDetailViewController ()

@end

@implementation TapActionOneDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //视图将要出现的时候刷新
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.goodStoreDetailModelArray = [NSMutableArray array];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];

    //导航栏标题
    self.navigationItem.title=@"品牌主页";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((WIDTH - 30) / 2, HEIGHT / 3);
    //设置item的行间距
    flowLayout.minimumLineSpacing = 10;
    //设置item的列间距
    flowLayout.minimumInteritemSpacing = 10;
    //设置item的上,左,下,右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //头部区域大小
    flowLayout.headerReferenceSize = CGSizeMake(WIDTH, HEIGHT / 3);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = RGBA(236, 235, 241, 1);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerClass:[GoodStoreDetailCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    //注册表头和底部视图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    
    self.ids = 1;//默认id为1
    [self getAFNetWorkingData:self.ids];//请求cell数据
    
    
}



-(void)getAFNetWorkingData:(NSInteger)page
{
    NSString *urlString = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/brands/%ld/items?limit=20&offset=0", self.goodStoreModel.id];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dictionary = responseObject;
        NSMutableArray *array = [[dictionary objectForKey:@"data"] objectForKey:@"items"];
        
        for (NSMutableDictionary *dic in array)
        {
            RecommenModel *goodStoreDetailModel = [[RecommenModel alloc] init];
            [goodStoreDetailModel setValuesForKeysWithDictionary:dic];
            
            goodStoreDetailModel.description1 = [dic objectForKey:@"description"];
            [self.goodStoreDetailModelArray addObject:goodStoreDetailModel];
            
        }
        
        [self.collectionView reloadData];
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             NSLog(@"error = %@",error);
             
         }];
    
}

//设置表头内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    
    //表头是图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:reusableView.bounds];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.goodStoreModel.cover_image_url]];
    [reusableView addSubview:imageView];
    return  reusableView;
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.goodStoreDetailModelArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //直接从cell重用池里取
    GoodStoreDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.layer.masksToBounds = YES;//masksToBounds裁剪掉边框
    cell.layer.cornerRadius = 5;//设置圆角度
    
    RecommenModel *goodStoreDetailModel = [self.goodStoreDetailModelArray objectAtIndex:indexPath.row];
    cell.goodStoreDetailModel = goodStoreDetailModel;
    
    return cell;
}

//点击item触发方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecommenDetailViewController  *recommenDetailVc=[[RecommenDetailViewController alloc]init];
       RecommenModel *model = [self.goodStoreDetailModelArray objectAtIndex:indexPath.row];
    recommenDetailVc.model=model;
    recommenDetailVc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:recommenDetailVc animated:YES];
 
    
}

-(void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
